<template>
  <div class="scam-list-page">
    <div class="section-content">
      <div class="search-section">
        <h2>骗子信息查询</h2>
        <div class="search-box">
          <el-input
            v-model="searchQuery"
            placeholder="输入手机号码或关键词进行查询"
            class="search-input"
            clearable
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          <el-button type="primary" @click="handleSearch" :loading="loading">
            查询
          </el-button>
        </div>
      </div>

      <div class="filter-section">
        <el-select v-model="sortBy" placeholder="排序方式" class="filter-select" @change="handleSearch">
          <el-option label="最新发布" value="latest" />
          <el-option label="最多浏览" value="most_viewed" />
          <el-option label="最多评论" value="most_commented" />
        </el-select>
      </div>

      <div v-if="loading" class="loading-section">
        <el-skeleton :rows="3" animated />
      </div>
      <div v-else>
        <div v-if="scamList.length === 0" class="empty-state">
          <el-empty description="暂无数据" />
        </div>
        <div v-else class="scam-grid">
          <div v-for="scam in scamList" :key="scam.id" class="scam-card">
            <div class="scam-header">
              <h3>{{ scam.title }}</h3>
              <el-tag :type="getStatusType(scam.status)" size="small">
                {{ getStatusText(scam.status) }}
              </el-tag>
            </div>
            
            <p class="scam-phone">
              <el-icon><Phone /></el-icon>
              {{ scam.phoneNumber }}
            </p>
            
            <p class="scam-description">{{ scam.description }}</p>
            
            <div class="scam-meta">
              <span>
                <el-icon><View /></el-icon>
                {{ scam.views || 0 }}
              </span>
              <span>
                <el-icon><ChatDotRound /></el-icon>
                {{ scam.commentCount || 0 }}
              </span>
              <span>
                <el-icon><Calendar /></el-icon>
                {{ formatDate(scam.createdAt) }}
              </span>
            </div>
            
            <div class="scam-footer">
              <router-link :to="`/scams/${scam.id}`" class="btn btn-text">
                查看详情
              </router-link>
            </div>
          </div>
        </div>

        <div class="pagination-section">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :total="total"
            :page-sizes="[10, 20, 30, 50]"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { scams } from '../api'
import { ElMessage } from 'element-plus'
import { Search, Phone, View, ChatDotRound, Calendar } from '@element-plus/icons-vue'

const searchQuery = ref('')
const sortBy = ref('latest')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const scamList = ref([])
const loading = ref(false)

const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

const getStatusType = (status) => {
  const types = {
    'verified': 'success',
    'pending': 'warning',
    'rejected': 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    'verified': '已验证',
    'pending': '待审核',
    'rejected': '已拒绝'
  }
  return texts[status] || '未知'
}

const fetchScams = async () => {
  loading.value = true
  try {
    const response = await scams.getList({
      page: currentPage.value,
      limit: pageSize.value,
      sort: sortBy.value,
      search: searchQuery.value
    })
    scamList.value = response.data
    total.value = response.total
  } catch (error) {
    ElMessage.error('获取数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  currentPage.value = 1
  fetchScams()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchScams()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchScams()
}

onMounted(() => {
  fetchScams()
})
</script>

<style scoped>
.scam-list-page {
  padding: 40px 0;
  background-color: #f8f9fa;
  min-height: calc(100vh - 72px);
}

.section-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.search-section {
  text-align: center;
  margin-bottom: 40px;
}

.search-section h2 {
  font-size: 2rem;
  margin-bottom: 20px;
  color: #2c3e50;
}

.search-box {
  display: flex;
  gap: 15px;
  max-width: 600px;
  margin: 0 auto;
}

.search-input {
  flex: 1;
}

.filter-section {
  margin-bottom: 30px;
  display: flex;
  justify-content: flex-end;
}

.filter-select {
  width: 150px;
}

.loading-section {
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.scam-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.scam-card {
  background: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  border: 1px solid #edf2f7;
}

.scam-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.scam-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.scam-header h3 {
  margin: 0;
  font-size: 1.1rem;
  color: #2c3e50;
  font-weight: 600;
}

.scam-phone {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
  color: #3498db;
  margin-bottom: 15px;
}

.scam-description {
  color: #64748b;
  line-height: 1.6;
  margin-bottom: 20px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 0.95rem;
}

.scam-meta {
  display: flex;
  gap: 20px;
  color: #64748b;
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.scam-meta span {
  display: flex;
  align-items: center;
  gap: 5px;
}

.scam-footer {
  display: flex;
  justify-content: flex-end;
}

.btn-text {
  color: #3498db;
  padding: 8px 16px;
  font-weight: 500;
  text-decoration: none;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.btn-text:hover {
  background: #f8f9fa;
}

.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.empty-state {
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  text-align: center;
}

@media (max-width: 768px) {
  .scam-list-page {
    padding: 20px 0;
  }

  .search-section h2 {
    font-size: 1.5rem;
  }

  .search-box {
    flex-direction: column;
  }

  .scam-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .scam-meta {
    flex-wrap: wrap;
    gap: 15px;
  }
}
</style> 